 #include "sessionfriendarea.h"
#include "debug.h"
#include "model/data.h"

#include <QScrollBar>
#include <QVBoxLayout>
#include <QPushButton>

#include <QStyleOption>
#include <QPainter>

/*
 * 滚动区实现
 *
 */
SessionFriendArea::SessionFriendArea(QWidget *parent)
    : QScrollArea{parent}
{
    // 该属性开启滚动效果
    this->setWidgetResizable(true);
    // 设置滚动条样式
    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(46, 46, 46);}");
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px;}");
    this->setStyleSheet("QWidget {border: none};");

    container = new QWidget();
    container->setFixedWidth(310);
    this->setWidget(container);

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignTop);
    container->setLayout(layout);

    // for(int i= 0 ; i< 40; i++){
    //     QPushButton* btn = new QPushButton();
    //     btn->setText("ann");
    //     layout->addWidget(btn);
    // }
#if TEST_UI
    QIcon icon(":/resource/image/defaultAvatar.png");
    for(int i = 0; i < 30; i++){
        this->addIem(ApplyItemType, QString::number(i), icon, "张三" + QString::number(i), "信息" + QString::number(i));
    }
#endif

}

void SessionFriendArea::clear()
{
    QLayout* layout = container->layout();
    for(int i = layout->count() - 1; i >= 0; i--){
        QLayoutItem* item = layout->takeAt(i);
        // 释放--内存泄漏
        if(item->widget()){
            delete item->widget();
        }
    }
}

void SessionFriendArea::addIem(ItemType itemType, const QString& id, const QIcon &avatar, const QString &name, const QString &text)
{
    SessionFriendItem* item = nullptr;
    if(itemType == SessionItemType){
        item = new SessionItem(this, id, avatar, name, text);
    }
    else if(itemType == FriendItemType){
        item = new FriendItem(this, id, avatar, name, text);
    }
    else if(itemType == ApplyItemType){
        item = new ApplyItem(this, id, avatar, name);
    }
    else{
        LOG() << "Item类型错误";
        return;
    }
    container->layout()->addWidget(item);
}

void SessionFriendArea::clickItem(int index)
{
    if(index < 0 || index >= container->layout()->count()){
        LOG() << "点击元素下标超限";
        return;
    }
    QLayoutItem* layoutItem = container->layout()->itemAt(index);
    if(layoutItem == nullptr || layoutItem->widget() == nullptr){
        LOG() << "指定的元素不存在";
        return;
    }
    SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(layoutItem->widget());
    item->select();
}



/*
 * 滚动区Item实现
 *
 */
SessionFriendItem::SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& text)
:owner(owner)
{
    this->setFixedHeight(70);
    this->setStyleSheet("QWidget { background-color: rgb(231, 231, 231);}");

    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(10, 0, 0, 0);
    layout->setHorizontalSpacing(10);
    layout->setVerticalSpacing(0);
    this->setLayout(layout);

    // 头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50, 50);
    avatarBtn->setIconSize((QSize(50, 50)));
    avatarBtn->setIcon(avatar);
    avatarBtn->setStyleSheet("QPushButton {border: none}");
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 名字
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(name);
    nameLabel->setStyleSheet("QLabel { font-size: 18px; font-weight: 600;}");
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 信息预览
    messageLabel = new QLabel();
    messageLabel->setText(text);
    messageLabel->setFixedHeight(35);
    messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 占0，0位置，据2行2列
    layout->addWidget(avatarBtn, 0, 0, 2, 2);
    layout->addWidget(nameLabel, 0, 2, 1, 8);
    layout->addWidget(messageLabel, 1, 2, 1, 8);
}

void SessionFriendItem::paintEvent(QPaintEvent *event)
{
    (void)event;
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void SessionFriendItem::mousePressEvent(QMouseEvent *event)
{
    (void)event;
    select();
}

void SessionFriendItem::enterEvent(QEnterEvent *event)
{
    (void)event;
    if(this->selected){
        return;
    }
    this->setStyleSheet("QWidget { background-color: rgb(215, 215, 215);}");
}

void SessionFriendItem::leaveEvent(QEvent *event)
{
    (void)event;
    if(this->selected){
        return;
    }
    this->setStyleSheet("QWidget { background-color: rgb(231, 231, 231);}");
}

void SessionFriendItem::select()
{
    const QObjectList children = this->parentWidget()->children();
    for(QObject* child : children){
        if(!child->isWidgetType()){
            continue;
        }
        SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(child);
        if(item->selected){
            item->selected = false;
            item->setStyleSheet("QWidget { background-color: rgb(231, 231, 231);}");
        }
    }

    this->setStyleSheet("QWidget { background-color: rgb(210, 210, 210);}");
    this->selected = true;
    this->active();
}

void SessionFriendItem::active()
{
//
}



/*
 *
 * 会话Item实现
 */
SessionItem::SessionItem(QWidget *owner, const QString &chatSessionId, const QIcon &avatar, const QString &name, const QString &lastMessage)
    :SessionFriendItem(owner, avatar, name, lastMessage), chatSessionId(chatSessionId)
{}

void SessionItem::active()
{
    LOG() << "点击了SessionItem触发逻辑 id: " << chatSessionId;
}



/*
 *
 * 好友Item实现
 */
FriendItem::FriendItem(QWidget *owner, const QString &userId, const QIcon &avatar, const QString &name, const QString &description)
    :SessionFriendItem(owner, avatar, name, description), userId(userId)
{

}

void FriendItem::active()
{
    // 加载会话的历史消息列表
    LOG() << "点击了FriendItem触发逻辑 id: " << userId;
}


/*
 *
 * 好友申请Item实现
 */
ApplyItem::ApplyItem(QWidget *owner, const QString &userId, const QIcon &avatar, const QString &name)
    :SessionFriendItem(owner, avatar, name, ""), userId(userId)
{
    // 移除父类的messageLabel
    QGridLayout* layout = dynamic_cast<QGridLayout*>(this->layout());
    layout->removeWidget(messageLabel);
    delete messageLabel;

    QPushButton* acceptBtn = new QPushButton();
    acceptBtn->setText("同意");
    QPushButton* rejectBtn = new QPushButton();
    rejectBtn->setText("拒绝");

    // 添加布局管理器中
    layout->addWidget(acceptBtn, 1, 2, 1, 1);
    layout->addWidget(rejectBtn, 1, 3, 1, 1);
}

void ApplyItem::active()
{
    LOG() << "点击了ApplyItem触发逻辑 id: " << userId;
}











